/**
	@author: Trương Hoàng Linh
	+ Class bao gồm: 
		Load tài nguyên game
		Hàm Tick
		Khi tải xong tài nguyên tự động gọi đến class Welcome Scene
*/
var mouseCoordinator;

(function (window){
	var _platformeInstance;
	function PlatformerGame (stage, gameWidth, gameHeight) {
		mouseCoordinator = new Text('', 'bold 14px Arial', '#FFF');
		mouseCoordinator.x = 600;
		mouseCoordinator.y = 10;

		_platformeInstance = this; 
		this.platformerGameStage = stage;
		this.gameWidth = gameWidth;
		this.gameHeight = gameHeight;
		var instance = this;

		this.WelcomeScene=null;

		this.platformerGameStage.addChild(mouseCoordinator);

		//flag
		this.downloadContentComplete = false;
	
	};

	//Update function game
	PlatformerGame.prototype.tick = function(event) {
		stage.update();
	}

	//Starting the game
	PlatformerGame.prototype.startGame = function() {
		console.log("Khởi động ticker");


		createjs.Ticker.timingMode = createjs.Ticker.RAF;
		createjs.Ticker.addEventListener("tick", this.tick);

		//download all needed images resources
		contentmanager = new ContentManager(this.platformerGameStage, this.gameWidth, this.gameHeight);
		contentmanager.SetDownloadCompleted(this.loadContent);


		contentmanager.StartDownload();	
	}

	//Load
	PlatformerGame.prototype.loadContent = function() {
		_platformeInstance.platformerGameStage.removeAllChildren();
		console.log("Hoàn thành tải các màn chơi");
		_platformeInstance.downloadContentComplete = true;
		console.log("load các màn chơi:..");
		
		loading = new Loading(_platformeInstance.gameWidth, _platformeInstance.gameHeight);
		loading.initialize();
		
		facebookinterface = new FacebookInterface(_platformeInstance.gameWidth, _platformeInstance.gameHeight);
		facebookinterface.initialize();
		
		
		ingame = new inGame(_platformeInstance.gameWidth, _platformeInstance.gameHeight); 
		resultscene = new resultContainer(_platformeInstance.gameWidth, _platformeInstance.gameHeight);
		resultscene.initialize();
		_platformeInstance.WelcomeScene = new WelcomeScene(_platformeInstance.platformerGameStage, contentmanager, _platformeInstance.gameWidth, _platformeInstance.gameHeight);
		
		_platformeInstance.platformerGameStage.addChild(loading);
		_platformeInstance.CheckConnectFacebook();
	} 

	//Hàm này nhiệm vụ , chờ kiếm tra việc kết nối, nếu chưa kết nối thì  chuyển sàng FacebookInterface
	PlatformerGame.prototype.CheckConnectFacebook= function() {
		if (window.checkFacebook) {
		    if ( !window.connectFacebook ) {
				_platformeInstance.platformerGameStage.removeAllChildren();
				_platformeInstance.platformerGameStage.addChild(facebookinterface);
			}
			_platformeInstance.getDataUser();
		} else {	
			window.setTimeout(_platformeInstance.CheckConnectFacebook, 100);
		}
	}
	
	PlatformerGame.prototype.getDataUser= function() {
		if ( window.isGetDataUser ) {
			_platformeInstance.platformerGameStage.removeAllChildren();
			_platformeInstance.WelcomeScene.addStage();
		} else {	
			window.setTimeout(_platformeInstance.getDataUser, 100);
		}
	}
	

	PlatformerGame.prototype.handleKeyDown = function(e) {
		this.WelcomeScene.handleKeyDown();
	}



	window.PlatformerGame = PlatformerGame;
} (window));
